/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：DxpRouterMessageReceiver.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.cneport.transfer.constant.RouterTypeConstants;
import com.cneport.transfer.message.dxp.DxpCtrl;
import com.cneport.transfer.message.dxp.DxpCtrlRep;
import com.cneport.transfer.message.dxp.DxpCtrl.RouteList.RouteInfo;
import com.cneport.transfer.router.api.IMessageReceiver;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.support.service.RouterService;
import com.cneport.transfer.router.support.service.SequenceService;
import com.cneport.transfer.util.DateUtil;
import com.cneport.transfer.util.XmlSerializer;

public class DxpRouterMessageReceiver implements IMessageReceiver {
    private static Logger logger = Logger
            .getLogger(DxpRouterMessageReceiver.class);
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private RouterService routerService;
    public boolean ctrlMessage2DB(List<Router> routerList) {
        routerService.deleteRouter(RouterTypeConstants.AXAWAY_ROUTE);
        int addRouterNum = routerService.insertRouter(routerList);
        if (routerList.size() == addRouterNum) {
            logger.info("更新路由成功！添加路由记录条数为：" + addRouterNum);
            return true;
        } else {
            logger.error("更新路由失败！添加路由记录条数与应加路由记录条数不服");
            return false;
        }
    }

    public String routerMessageReceiver(String ctrlMessage) {
        DxpCtrl dxpCtrl = string2Object(ctrlMessage);
        List<Router> routerList = dxpCtrl2Router(dxpCtrl);
        DxpCtrlRep dxpCtrlRep = new DxpCtrlRep();
        dxpCtrlRep.setMsgId(dxpCtrl.getMsgId());
        dxpCtrlRep.setOriMsgId(dxpCtrl.getMsgId());
        if (ctrlMessage2DB(routerList)) {
            dxpCtrlRep.setNote(null);
            dxpCtrlRep.setRepCode(1);
            dxpCtrlRep.setRepInfo("成功");
        } else {
            dxpCtrlRep.setNote(null);
            dxpCtrlRep.setRepCode(-1);
            dxpCtrlRep.setRepInfo("添加删除失败！");
        }
        String dxpCtrlRepMessage;
        try {
            dxpCtrlRepMessage = XmlSerializer.serialize(dxpCtrlRep);
        } catch (Exception e) {
            logger.error("路由控制应答报文序列号错误！");
            return null;
        }
        return dxpCtrlRepMessage;
    }

    public DxpCtrl string2Object(String ctrlMessage) {
        Object obj;
        try {
            obj = XmlSerializer.deSerialize(ctrlMessage, DxpCtrl.class);
            DxpCtrl dxpCtrl = (DxpCtrl) obj;
            return dxpCtrl;
        } catch (Exception e) {
            logger.error("路由控制报文解析错误！");
            return null;
        }

    }
    
    public List<Router> dxpCtrl2Router(DxpCtrl dxpCtrl) {
        List<Router> routerList = new ArrayList<Router>();
        List<RouteInfo> routerInfoList = dxpCtrl.getRouteList().getRouteInfo();
        for (DxpCtrl.RouteList.RouteInfo routerInfo : routerInfoList) {
            Router router = new Router();
            router.setEndNode(routerInfo.getEndNode());
            router.setRouterEffectTime(DateUtil.convert2Date(dxpCtrl
                    .getRouteList().getEfficientTime()));
            router.setRouterExpression(routerInfo.getNextNode());
            router.setRouterId(sequenceService.getSequence("routerID"));
            router.setRouterType(RouterTypeConstants.DTF_ROUTE);
            router.setRouterVer(dxpCtrl.getRouteList().getVersion());
            router.setStartNode(dxpCtrl.getRecNodeId());
            routerList.add(router);
        }
        return routerList;

    }
    
}
